package com.rzjm.exercise.module.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rzjm.exercise.common.base.controller.BaseController;
import com.rzjm.exercise.common.base.data.DataTableLayUI;
import com.rzjm.exercise.common.log.annotation.Log;
import com.rzjm.exercise.common.utils.R;
import com.rzjm.exercise.module.domain.Shop;
import com.rzjm.exercise.module.domain.WxUser;
import com.rzjm.exercise.module.service.ShopService;
import com.rzjm.exercise.module.service.WxUserService;
import com.rzjm.exercise.sys.core.dict.annotation.DictBody;
import com.rzjm.exercise.sys.system.user.domain.UserDO;
import com.rzjm.exercise.sys.system.user.service.UserService;
import com.vdurmont.emoji.EmojiParser;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 微信用户
 *
 * @author mr.zhou
 * @email zhouwei_nice@163.com
 * @date 2018-12-15 22:20:07
 */

@Controller
@RequestMapping("/module/wxuser")
public class WxUserController extends BaseController {
    @Resource
    private WxUserService wxUserService;
    @Resource
    private UserService userService;
    @Resource
    private ShopService shopService;

    @Log("查询微信用户页面")
    @GetMapping()
    @RequiresPermissions("module:wxuser:wxuser")
    String RobotUser() {
        return "module/wxuser/wxuser";
    }


    @Log("查询微信用户")
    @ResponseBody
    @DictBody
    @GetMapping("/list")
    @RequiresPermissions("module:wxuser:wxuser")
    public DataTableLayUI<WxUser> list(DataTableLayUI<WxUser> condition) {
        UserDO user = getUser();
        String keyWord = condition.getSearchParams().get("search_like_nickName").toString();
        String platform = condition.getSearchParams().get("search_eq_platform").toString();
        Map<String,Object> map1 = new HashMap<>();
        if(user.getUserType() == 2){//片区管理员可以查看所有绑定在 旗下企业上的用户
            map1.put("jituan_user_id",user.getUserId());
        } else if(user.getUserType() == 3){//片区管理员可以查看所有绑定在 旗下企业上的用户
            map1.put("sys_user_id",user.getUserId());
        } else if(user.getUserType() == 4 || user.getUserType() == 5 || user.getUserType() == 6 || user.getUserType() == 7){
            map1.put("shop_id",user.getShopId());
        }
        if(StrUtil.isNotEmpty(platform)){
            map1.put("platform", platform);
        }

        map1.put("keyWord",keyWord);
        Page<WxUser> page = wxUserService.getPage(condition);
        List<WxUser> list = wxUserService.getPageByUserId(page,map1);
        list.forEach(wxuser -> {
            if(wxuser.getNickName()!=null){
                wxuser.setNickName(EmojiParser.parseToUnicode(wxuser.getNickName()));
            }
            Shop shop = shopService.findById(wxuser.getShopId());
            if(shop!=null){
                wxuser.setShopName(shop.getShopName());
            }
            UserDO userDO = userService.getById(wxuser.getSysUserId());
            if(userDO!=null){
                wxuser.setSysUserName(userDO.getName() + "("+userDO.getMobile()+")");
            }
        });
        DataTableLayUI<WxUser> dt = wxUserService.pageSearchCustom(page,list,condition);
        return dt;
    }

    @Log("编辑微信用户页面")
    @GetMapping("/edit/{id}")
    @RequiresPermissions("module:wxuser:edit")
    String edit(@PathVariable("id") Integer id, Model model) {
        WxUser wxUser = wxUserService.getById(id);
        model.addAttribute("wxuser", wxUser);
        return "module/wxuser/detail";
    }

    @Log("编辑微信用户页面")
    @GetMapping("/goSetUserType/{id}")
    @RequiresPermissions("module:wxuser:edit")
    String goSetUserType(Model model,@PathVariable("id") Integer id) {
        WxUser wxUser = wxUserService.getById(id);
        if(wxUser !=null && wxUser.getNickName()!=null){
            wxUser.setNickName(EmojiParser.parseToUnicode(wxUser.getNickName()));
        }
        model.addAttribute("wxuser", wxUser);
        return "module/wxuser/set_check_user";
    }


    @Log("编辑微信用户页面")
    @GetMapping("/goBandAdmin/{id}")
    @RequiresPermissions("module:wxuser:edit")
    String goBandAdmin(Model model,@PathVariable("id") Integer id) {
        WxUser wxUser = wxUserService.getById(id);
        if(wxUser !=null && wxUser.getNickName()!=null){
            wxUser.setNickName(EmojiParser.parseToUnicode(wxUser.getNickName()));
        }
        model.addAttribute("wxuser", wxUser);
        List<UserDO> userDOS = userService.selectAllList();
        model.addAttribute("users", userDOS);
        return "module/wxuser/band_admin";
    }

    /**
     * 修改
     */
    @Log("更新微信用户")
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("module:wxuser:edit")
    public R update(@Valid WxUser wxUser) {
        UserDO user = userService.get(wxUser.getSysUserId());
        if(user!=null){
            wxUser.setShopId(user.getShopId());
        }
        wxUserService.updateById(wxUser);
        return R.ok();
    }



    /**
     * 修改
     */
    @Log("更新微信用户")
    @ResponseBody
    @RequestMapping("/updateTestUser")
    @RequiresPermissions("module:wxuser:edit")
    public R updateTestUser(@Valid WxUser wxUser) {
        wxUserService.updateById(wxUser);
        return R.ok();
    }

    /**
     * 修改
     */
    @Log("更新微信用户")
    @ResponseBody
    @RequestMapping("/clearBandInfo")
    @RequiresPermissions("module:wxuser:edit")
    public R clearBandInfo(@Valid WxUser wxUser) {

        if(wxUser.getClearType() == 0){
            wxUser.setShopId(0);
        }
        if(wxUser.getClearType() == 1){
            wxUser.setSysUserId(0);
        }
        wxUserService.updateById(wxUser);
        return R.ok();
    }
}
